﻿using LightingPlatform.Tools;
using LightingPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LightingPlatform
{
    public partial class LoginWindow : ChildWindow
    {
        public LoginWindow()
        {
            InitializeComponent();
            LoginBtn.Click += OKButton_Click;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Login();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void UserPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Login();
            }
        }

        private async void Login()
        {
            if (string.IsNullOrEmpty(UserName.Text))
            {
                MessageHelper.ShowMessage("用户名不能为空。");
                return;
            }

            if (string.IsNullOrEmpty(UserPassword.Password))
            {
                MessageHelper.ShowMessage("密码不能为空。");
                return;
            }

            WebServiceProxy proxy = new WebServiceProxy();
            User user = await proxy.Login(UserName.Text, UserPassword.Password);
            if (user != null)
                BusinessContext.Singleton.User = user;

            this.DialogResult = true;
        }
    }
}

